(function($){
	$.fn.mySortTable = function(opts) {
		var settings = $.extend({
			begin:opts?opts.begin:"beginOrder",
			save:opts?opts.save:"saveOrder"
		},opts || {});
		var _isSort = false;
		var sortEle = $(this);
		var sortEleParent = $(this).parent();
		
		sortEle.sortable({
			axis:"y",
			helper:function(event,element) {
				var _original = element.children();
				var _helper = element.clone();
				_helper.children().each(function(index) {
					$(this).width(_original.eq(index).width());
				});
				_helper.css({"background":"#000","color":"#fff"});
				return _helper;
			},
			update:function(event,ui) {
				setOrders();
			}
		});
		
		sortEle.sortable("disable");	
		$("#"+settings.begin).on("click",beginOrders);
		$("#"+settings.save).click(saveOrders);
		
		$.checkAjax = function(data) {
			if(data.result) {
				return true;
			} else if(!data.result){
				alert(data.msg);
				return false;
			}
		};
		function beginOrders() {
			if(!_isSort) {
				$(sortEle).find("tr").hover(function() {
					$(this).css({"background":"#000","color":"#fff"});
				},function() {
					$(this).css({"background":"#ddd","color":"#000"});
				});
				sortEleParent.find("thead tr").append("<td class='centerTd'>序号</td>");
				setOrders();
				//$(".listTable tfoot tr").append("<td><a class='list_op' style='background:#007' href='#'>拖动排序</a></td>");
				sortEleParent.find("tfoot tr td").attr("colspan","7");
				sortEle.sortable("enable");
				sortEle.css("background","#99b");
				$(sortEle).find("tr").css({"background":"#ddd","color":"#000"});
				_isSort = true;
			} else {
				alert("当前处于排序状态，你可以拖动行进行排序");
			}
		}
		function saveOrders() {
			if(_isSort) {
				//alert(sortEle.sortable("toArray"));  需給每个tr设置一个id属性 会获取到以逗号隔开的id的字符串
				//id_${c.id } 使用下面的方式需要设置格式 直接传输一个id数组到服务器端
				//alert(sortEle.sortable("serialize",{key:"ids"}));
				$.getJSON("updateSort?"+sortEle.sortable("serialize",{key:"ids"}),function(data) {
					if($.checkAjax(data)) {
						$(sortEle).find("tr").unbind(); 
						$(sortEle).sortable("disable");	
						sortEleParent.find("thead td:last").remove();
						$(sortEle).find("tr td:last-child").remove();
						sortEle.css({"background":"#fff","color":"#000"});
						$(sortEle).find("tr").css({"background":"#fff","color":"#000"});
						_isSort = false;
						parent.refreshTree();
					}
				});
			} else {
				alert("当前处于未排序状态，不能存储排序");
			}
		}
		function setOrders() {
			sortEle.find("tr").each(function(index) {
				if(_isSort) {
					//$(this).find("td:last").html(index+1);
					$(this).children().last().html(index+1);
				} else { 
					$(this).append("<td class='centerTd'>"+(index+1)+"</td>");
				}
			});
		}
		return sortEle;
	};
	$.fn.mytree = function(opts) {
		var setting = $.extend({
				data: {
					simpleData: {
						enable: true,
						idKey: "id",
						pIdKey: "pid",
						rootPId: -1
					}
				},
				view: {
					selectedMulti:false,
					expandSpeed:200,
					dblClickExpand:false
				},
				callback: ({
					
				},opts?(opts.callback?opts.callback:{}):{}),
				check: ({
					enable:false
				},opts?(opts.check?opts.check:{}):{}),
				async: {
					enable: true,
					url:opts?(opts.url ||"treeAll"):"treeAll",
					type: "POST"
				}
			},opts||{});
		var t = $.fn.zTree.init($(this), setting);
		return t;
	};
	
	$.fn.myaccordion = function(opts) {
		var settings = $.extend({
			selectedClz:"navSelected",
			titleTagName:"h3"
		},opts||{});
		var titleNode = $(this).find("ul>"+settings.titleTagName);
		var selectedNode = $(this).find("ul."+settings.selectedClz+">"+settings.titleTagName);
		titleNode.css("cursor","pointer");
		titleNode.nextAll().css("display","none");
		selectedNode.nextAll().css("display","block");
		titleNode.click(function(){
			var checked = $(this).parent().hasClass(settings.selectedClz);
			if(checked) {
				$(this).parent().removeClass(settings.selectedClz);
				$(this).nextAll().slideUp("fast");
			} else {
				$(this).parent().parent().find("ul."+settings.selectedClz+">"+settings.titleTagName).nextAll().slideUp("fast");
				$(this).parent().parent().find("ul."+settings.selectedClz).removeClass(settings.selectedClz);
				$(this).parent().addClass(settings.selectedClz);
				$(this).nextAll().slideDown("fast");
			}
		});
	};
	
	$.fn.trColorChange = function(opts) {
		var settings = $.extend({
			overClz:"trMouseover",
			evenClz:"trEvenColor"
		},opts||{});
		$(this).find("tbody tr:even").addClass(settings.evenClz);
		$(this).find("tbody tr").on("mouseenter mouseleave",function(){
			$(this).toggleClass(settings.overClz);
		});
	};
	
	$.fn.confirmOperator = function(opts) {
		var settings = $.extend({
			msg:"该操作不可逆，确定进行该操作吗？",
			eventName:"click"
		},opts||{});
		$(this).on(settings.eventName,function(event){
			if(!confirm(settings.msg)) {
				event.preventDefault();
			}
		});
	};
})(jQuery);